<?php

namespace app\service;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsResponseBody;
use Darabonba\OpenApi\Models\Config;
use support\Cache;
use app\service\shansumaSms\Client;
use app\service\shansumaSms\Request;

class SmsService
{
    public static ?SmsService $_instance       = null;
    protected string          $accessKeyId     = 'LTAI5tBNAh92Q';
    protected string          $accessKeySecret = 'VbmuKy';

    /**
     * @return SmsService|mixed
     */
    public static function instance(): ?SmsService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @param string $mobile
     * @param string $type
     * @return SendSmsResponseBody
     */
    public function send_sms(string $mobile, string $type): SendSmsResponseBody
    {
        $config = new Config([
            "accessKeyId"     => $this->accessKeyId,
            "accessKeySecret" => $this->accessKeySecret,
        ]);
        $code   = rand(100001, 999999);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        $client           = new Dysmsapi($config);
        $sendSmsRequest   = new SendSmsRequest([
            "phoneNumbers"  => $mobile,
            "signName"      => "XXXXX",
            "templateCode"  => "SMS_20674",
            "templateParam" => "{\"code\":{$code}}",
        ]);

        $_rs = $client->sendSms($sendSmsRequest);
        if ($_rs->body->code == 'OK') {
            $s_key = 'SMS_CODE_' . md5($mobile . $type);
            Cache::set($s_key, $code, 300); // 验证码保存5分钟
        }
        return $_rs->body;
    }

    public function send_sms_chuanglang(string $mobile, string $type)
    {
        //6位
//        $code = rand(100001, 999999);
        $code  = rand(1001, 9999);
        $clapi = new ChuanglanSmsApi();
        //设置您要发送的内容：其中“【】”中括号为运营商签名符号，多签名内容前置添加提交
        $sign = 'OwenWeb';
//        $msg = "【{$sign}】您的验证码为：{$sms_code}，请勿泄露于他人！";
        $msg    = "【{$sign}】您正在使用{$sign}，验证码：{$code}，有效时间为5分钟，请勿外泄。";
        $result = $clapi->sendSMS($mobile, $msg);
        $output = json_decode($result, true);

        $_rdata['code']    = 0;
        $_rdata['message'] = isset($output['errorMsg']) ? $output['errorMsg'] : "短信发送失败";

        if (isset($output['code'])) {
            if ($output['code'] == '0') {
                $_rdata['code']    = 'OK';
                $_rdata['message'] = '发送成功';
                $s_key             = 'SMS_CODE_' . md5($mobile . $type);
                Cache::set($s_key, $code, 300); // 验证码保存5分钟
            }
        }

        return (object)$_rdata;
    }


    public function send_sms_shansuma(string $mobile, string $type)
    {
        //6位
//        $code = rand(100001, 999999);
        $code = rand(1001, 9999);

        $client = new Client();
        $client->setAppId('OwenWeb_1122');                             //开发者ID，在【设置】-【开发设置】中获取
        $client->setSecretKey('369b2a02a7363fe25'); //开发者密钥，在【设置】-【开发设置】中获取

        $request = new Request();
        $request->setMethod('sms.message.send');
        $request->setBizContent([
            'mobile'      => [$mobile],                     //接受号码
            'template_id' => 'ST_202010007',    //模板id
            'type'        => 0,                       //验证码类型为0
            'sign'        => 'OwenWeb',             //这里填写短信签名，不需要填写签名id
            'send_time'   => '', //空是及时发送，否则定时发送	2019-04-15 12:20:30
            'params'      => [
                'code' => $code
            ]
        ]);
        list($ret, $errno, $errstr, $et) = $client->execute($request);

        $_rdata['code']    = 0;
        $_rdata['message'] = isset($ret['data']['message']) ? $ret['data']['message'] : "短信发送失败";
        if (isset($ret['data']['code'])) {
            if ($ret['data']['code'] == 0) {
                $_rdata['code']    = 'OK';
                $_rdata['message'] = '发送成功';
                $s_key             = 'SMS_CODE_' . md5($mobile . $type);
                Cache::set($s_key, $code, 300); // 验证码保存5分钟
            }
        }

        return (object)$_rdata;
    }

}